package com.dingding.order.core.cancel.dao.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.beans.UserTaxiOrdert;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.order.core.cancel.dao.ITaxiCancelOrderTDao;
import com.dingding.order.core.cancel.sql.CancelOrderSql;
import com.dingding.order.dto.cancel.PushAutoCancelTaxiOrderDTO;
import com.dingding.order.dto.cancel.PushTaxiCancelOrderDTO;

/**
 * 
 * @ClassName: TaxiCancelOrderTDaoImpl 
 * @Description: 出租车取消订单DAO实现类
 * @author YHQ
 * @date 2016年7月4日 上午11:32:02
 * @version V2.3.0
 * 
 */
@Component("taxiCancelOrderTDao")
public class TaxiCancelOrderTDaoImpl extends BaseDaoImpl<UserTaxiOrdert> implements ITaxiCancelOrderTDao{

	/** 
	 * 
	 * @Title: getUserOrder 
	 * @Description: 获取订单信息
	 * @param orderId  订单Id
	 * @author chenwei
	 * 
	 */
	@Override
	@Transactional(readOnly = true)
	public UserTaxiOrdert getUserOrder(String orderId) {

		// 根据订单ID查询订单信息
		UserTaxiOrdert taxiOrdert = null;

		try {
			// 添加查询条件
			Criterion criterion = Restrictions.eq("taxiOrdertId", orderId);
			taxiOrdert = super.queryCriteriaUnResult(criterion);
		} catch (Exception e) {
			log.error("获取订单信息出现异常！", e);
		}

		return taxiOrdert;
	}

	/**
	 * 
	 * @Title: deleteUserTaxiOrder 
	 * @Description: 删除出租车订单信息
	 * @param @return    设定文件 
	 * @author YHQ 
	 * @version V2.3.0
	 * @return boolean    返回类型
	 * 
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean deleteUserTaxiOrder(UserTaxiOrdert taxiOrdert) {
		
		try {
			
			super.delete(taxiOrdert);
			
			return true;
		} catch (Exception e) {
			log.error("删除出租车订单信息时出现异常  类："+this.getClass()+" deleteUserTaxiOrder()", e);
		}
		
		return false;
	}
	
	
	/**
	 * @Title: getPushTaxiCancelOrderInfo 
	 * @Description: 用户取消订单-获取给司机发送短信和推送的消息 
	 * @param cancelUserId 取消订单用户Id
	 * @param pushUserId 推送用户Id
	 * @author yhq 
	 * @return PushCancelOrder 返回类型 
	 * @throws
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public PushTaxiCancelOrderDTO getPushTaxiCancelOrderInfo(String cancelUserId, String pushUserId,Integer cancelType) {

		// 创建乘客支付成功，给司机推送Bean
		PushTaxiCancelOrderDTO pushTaxiCancelOrderDTO = new PushTaxiCancelOrderDTO();

		// 根据司机Id,获取司机信息
		Object[] obj = null;
		try {
			
			if(cancelType == 1){
				obj = super.sqlQueryUnResultObj(CancelOrderSql.PASSENGER_CANCEL_TAXI_ORDER, cancelUserId, pushUserId);
			}else if(cancelType == 2){
				obj = super.sqlQueryUnResultObj(CancelOrderSql.DRIVER_CANCEL_TAXI_ORDER, cancelUserId, pushUserId);
			}
		} catch (Exception e) {
			log.info("取消出租车订单获取推送内容时出现异常  类：" + this.getClass() + " getPassengerPayPushInfo()", e);
		}

		// 判断司机信息是否为空
		if (obj != null && obj.length > 0) {

			// 将司机信息赋值给pushPassengerPay
			this.getPushCancelOrder(obj, pushTaxiCancelOrderDTO);
		}

		return pushTaxiCancelOrderDTO;
	}

	/** 
	 * @Title: getPushCancelOrder 
	 * @Description: 将Object[]类型转为pushConfirmArrival
	 * @param @param obj 司机信息 
	 * @param @param pushCancelOrder  推送对象
	 * @author chenwei
	 * @return void
	 * @throws 
	*/
	public void getPushCancelOrder(Object[] obj, PushTaxiCancelOrderDTO pushTaxiCancelOrderDTO) {

		// 司机Id(推送Id)
		if (obj[0] != null) {
			pushTaxiCancelOrderDTO.setDriverId(obj[0].toString());
		}
		// 设备类型
		if (obj[1] != null) {
			// 设备类型 1:iOS 2:Android
			String userType = obj[1].toString();

			if (userType.equals("1") || userType.equals("iOS")) { // 设备类型为：iOS
				pushTaxiCancelOrderDTO.setLoginEquipment("iOS");
			} else if (userType.equals("2") || userType.equals("Android")) { // 设备类型为：Android
				pushTaxiCancelOrderDTO.setLoginEquipment("Android");
			}

		}
		// 司机电话
		if (obj[2] != null) {
			pushTaxiCancelOrderDTO.setTellPhone(obj[2].toString());
		}
		// 乘客姓名
		if (obj[3] != null) {
			pushTaxiCancelOrderDTO.setPassengerName(obj[3].toString());
		}
		// 司机名称
		if (obj[4] != null) {
			pushTaxiCancelOrderDTO.setDriverName(obj[4].toString());
		}
		// 车牌号
		if (obj[5] != null) {
			pushTaxiCancelOrderDTO.setLisencePlate(obj[5].toString());
		}

	}

	
	/**
	 * 
	 * @Title: geTaxiOrderts 
	 * @Description: 获取自动取消出租车订单列表 
	 * @author YHQ 
	 * @version V2.3.0
	 * @return List<UserTaxiOrdert>    返回类型
	 * 
	 */
	@Override
	@Transactional(readOnly=true)
	public List<UserTaxiOrdert> geTaxiOrderts() {
		
		// 获取待自动取消订单列表
		List<Object[]> objects = super.sqlQueryListObj(CancelOrderSql.AUTO_CANCEL_TAXI_ORDER_LIST, false);
		
		// 将待自动取消订单列表转为类型为List<UserTaxiOrdert>的集合中
		if(objects != null && objects.size() > 0){
			
			return getUserOrders(objects);
		}
		
		return null;
	}

	
	/**
	 * 
	 * @Title: getUserOrders 
	 * @Description: 自动取消订单信息 
	 * @param objects 订单信息
	 * @author YHQ 
	 * @return void    返回类型
	 * 
	 */
	public List<UserTaxiOrdert> getUserOrders(List<Object[]> objects){
		
		List<UserTaxiOrdert> taxiOrderts = new ArrayList<UserTaxiOrdert>();
		
		for (Object[] obj : objects) {
			
			UserTaxiOrdert taxiOrdert = new UserTaxiOrdert();
			
			if(obj[0] != null){
				taxiOrdert.setTaxiOrdertId(obj[0].toString());
			}
			if(obj[1] != null){
				taxiOrdert.setPlaceOrderUserId(obj[1].toString());
			}
			if(obj[2] != null){
				taxiOrdert.setPlaceOrderCity(obj[2].toString());
			}
			if(obj[3] != null){
				taxiOrdert.setSetOutTime((Date)obj[3]);
			}
			if(obj[4] != null){
				taxiOrdert.setStartAddress(obj[4].toString());
			}
			if(obj[5] != null){
				taxiOrdert.setStartLongitude(ToolUtil.castDouble(obj[5]));
			}
			if(obj[6] != null){
				taxiOrdert.setStartLatitude(ToolUtil.castDouble(obj[6]));
			}
			if(obj[7] != null){
				taxiOrdert.setEndAddress(obj[7].toString());
			}
			if(obj[8] != null){
				taxiOrdert.setEndLongitude(ToolUtil.castDouble(obj[8]));
			}
			if(obj[9] != null){
				taxiOrdert.setEndLatitude(ToolUtil.castDouble(obj[9]));
			}
			if(obj[10] != null){
				taxiOrdert.setDistance(new BigDecimal(obj[10].toString()));
			}
			if(obj[11] != null){
				taxiOrdert.setPayMoney(new BigDecimal(obj[11].toString()));
			}
			if(obj[12] != null){
				taxiOrdert.setAlipayMoney(new BigDecimal(obj[12].toString()));
			}
			if(obj[13] != null){
				taxiOrdert.setPayType(ToolUtil.castInteger(obj[13]));
			}
			if(obj[14] != null){
				taxiOrdert.setTotalMoney(new BigDecimal(obj[14].toString()));
			}
			if(obj[15] != null){
				taxiOrdert.setNumber(ToolUtil.castInteger(obj[15]));
			}
			if(obj[16] != null){
				taxiOrdert.setMessage(obj[16].toString());
			}
			if(obj[17] != null){
				taxiOrdert.setPayDistance(ToolUtil.castDouble(obj[17]));
			}
			if(obj[18] != null){
				taxiOrdert.setGrabOrderUserId(obj[18].toString());
			}
			if(obj[19] != null){
				taxiOrdert.setGrabOrderTime((Date)obj[19]);
			}
			if(obj[20] != null){
				taxiOrdert.setOrderStatus(ToolUtil.castInteger(obj[20]));
			}
			if(obj[21] != null){
				taxiOrdert.setCreateTime((Date)obj[21]);
			}
			if(obj[22] != null){
				taxiOrdert.setPayTime((Date)obj[22]);
			}
			if(obj[23] != null){
				taxiOrdert.setEndTime((Date)obj[23]);
			}
			if(obj[24] != null){
				taxiOrdert.setCancelTime((Date)obj[24]);
			}
			if(obj[25] != null){
				taxiOrdert.setOrderType(ToolUtil.castInteger(obj[25]));
			}
			if(obj[26] != null){
				taxiOrdert.setCancelType(ToolUtil.castInteger(obj[26]));
			}
			if(obj[27] != null){
				taxiOrdert.setCancelDesc(obj[27].toString());
			}      
			if(obj[28] != null){
				taxiOrdert.setCancelUserId(obj[28].toString());
			}      
			if(obj[29] != null){
				taxiOrdert.setIsComment(ToolUtil.castInteger(obj[29]));
			}      
			if(obj[30] != null){
				taxiOrdert.setTransactionType(ToolUtil.castInteger(obj[30]));;
			}      
			
			taxiOrderts.add(taxiOrdert);
		}
		
		return taxiOrderts;
	}

	/**
	 * 
	 * @Title: getPushCancelTaxiOrderInfo 
	 * @Description: 出租车订单自动取消推送消息 
	 * @param userId 用户Id
	 * @author YHQ 
	 * @version V2.3.0
	 * @return PushAutoCancelTaxiOrderDTO    返回类型
	 * 
	 */
	@Override
	@Transactional(readOnly=true)
	public PushAutoCancelTaxiOrderDTO getPushCancelTaxiOrderInfo(String userId) {
		
		// 获取出租车订单自动取消推送消息 
		Object[] obj = super.sqlQueryUnResultObj(CancelOrderSql.GET_TAXI_PUSH_INFO, userId);
		
		// 获取PushAutoCancelTaxiOrderDTO类型的推送消息内容
		if(obj != null && obj.length > 0){
			
			return geAutoCancelTaxiOrderDTO(obj);
		}
		
		return null;
	}

	/**
	 * 
	 * @Title: geAutoCancelTaxiOrderDTO 
	 * @Description: 将推送信息转为 PushAutoCancelTaxiOrderDTO类型
	 * @param obj 推送信息
	 * @author YHQ 
	 * @version V2.3.0
	 * @return PushAutoCancelTaxiOrderDTO    返回类型
	 * 
	 */
	public PushAutoCancelTaxiOrderDTO geAutoCancelTaxiOrderDTO(Object[] obj){
		
		PushAutoCancelTaxiOrderDTO autoCancelTaxiOrderDTO = new PushAutoCancelTaxiOrderDTO();
		
		if(obj[0] != null){
			autoCancelTaxiOrderDTO.setAliasId(obj[0].toString());
		}
		if(obj[1] != null){
			
			if(ToolUtil.castInteger(obj[1]) == 1){
				autoCancelTaxiOrderDTO.setLoginEquipment("iOS");
			}else if(ToolUtil.castInteger(obj[1]) == 2){
				autoCancelTaxiOrderDTO.setLoginEquipment("Android");
			}
			
		}
		if(obj[2] != null){
			autoCancelTaxiOrderDTO.setTellPhone(obj[2].toString());
		}
		
		return autoCancelTaxiOrderDTO;
	}
	
}
